<?php

class ImagesClass {

	function ImagesClass () {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}
	
	function flash_sel($id, $paging=false) {
		$rows = array();
		if ($paging === false){
			if ($id > 0) $query="select * FROM `".$this->setupdata['table_prefix']."files` WHERE parent=".$id." and types = 'flash' ORDER BY `save_date` DESC";
			else $query="select * FROM `".$this->setupdata['table_prefix']."files` WHERE types = 'flash' ORDER BY `save_date` DESC";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
				$row['info'] = unserialize($row['info']);
				array_push($rows, $row);
			}
			return $rows;
		} else {
			$from = $paging['curent_page'] * $paging['items_per_page'];
			if ($id > 0) $str = " parent=".$id." and ";
			else $str = "";
			$query="SELECT * FROM `".$this->setupdata['table_prefix']."files` WHERE ".$str."types = 'flash' ORDER BY `save_date` DESC LIMIT ".$from.", ".$paging['items_per_page'];
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
				$row['info'] = unserialize($row['info']);
				$rows['list'][] =  $row;
			}
			$query="SELECT COUNT(`id`) FROM `".$this->setupdata['table_prefix']."files` WHERE ".$str."types = 'flash'";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_fetch_array($sql);
			$rows['count'] = $row[0];
			return $rows;
		}
	}

	function flash_add($inf, $name, $link, $parent, $w, $h, $usr, $rewrite=0) {
		$dat = date("YmdHis");
		$info['name'] = $link;
		$info['type'] = $inf['type'];
		$info['size'] = $inf['size'];
		$info['height'] = $h;
		$info['width'] = $w;
		$info = serialize($info);
		$name = addslashes($name);
		if ($rewrite != 0) {
			$sql = $this->mysqlclass->mysql_my_query("select id FROM `".$this->setupdata['table_prefix']."files` where parent = '$parent' and types='flash'", __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_num_rows($sql);
			if ($row != 0) {
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$this->flash_del($parent, "", $row['id']);
			}
		}		
		$sql = "INSERT INTO `".$this->setupdata['table_prefix']."files` (`id`,`parent`,`name`,`info`,`save_date`,`owner`, `types`)	VALUES ('', '$parent', '$name', '$info', '$dat', '$usr', 'flash')";
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		$i = $this->mysqlclass->mysql_my_insert_id();
		return $i;
	}
	
	function flash_del_with_parent_data($parent, $name, $image_id){
		global $treeclass;
		if ($name == ""){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `name` FROM `".$this->setupdata['table_prefix']."data` where parent = '".$parent."' and value ='".$id."' and type='flash'", __LINE__, __FILE__);
			$num = $this->mysqlclass->mysql_my_num_rows($sql);
			if ($num == 1){
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$treeclass->mod_item_data($parent, $row['name'], "");
			}
		} else $treeclass->mod_item_data($parent, $name, "");
		//$this->flash_del($image_id);
	}
	
	function flash_del($id) {
		global $setupdata, $treeclass;
		$id = intval($id);
		if ($id > 0) {
			$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."files` where id = '$id'", __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_num_rows($sql);
			if ($row != 0) {
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$parent = $row['parent'];
				$row = unserialize($row['info']);
				$files = $setupdata['binary_path']."/".$row['name'];
				if (!(file_exists($files))) {
					$setupdata['binary_path'] = $setupdata['binary_path']."/".$parent;
					$files = $setupdata['binary_path']."/".$row['name'];
				}
				if ((file_exists($files)) and (strlen($row['name']) > 0)){
					list($par, $d, $s) = explode("-", $row['name']);
					$pattern = $par."-".$d;
					if (is_dir($setupdata['binary_path'])){
						$d = opendir($setupdata['binary_path']);
						while ($file = readdir($d)){
							if (is_file($setupdata['binary_path']."/".$file)) {
								if (strpos($file, $pattern) === FALSE){
								} else{
									unlink($setupdata['binary_path']."/".$file);
								}
							}
						}
						closedir($d);
					}
				}
				$tmp = round($id / 250);
				$path_to_cache_file = $this->setupdata['binary_path']."/ids"."/".$tmp."/".$id;
				if (file_exists($path_to_cache_file)) unlink($path_to_cache_file);

				if (file_exists($files) === FALSE ){
					$this->mysqlclass->mysql_my_query("delete from `".$this->setupdata['table_prefix']."files` where id = $id", __LINE__, __FILE__);
				}
			}
		}
	}
	
	function image_add($inf, $name, $link, $parent, $w, $h, $usr, $rewrite=0) {
		$dat = date("YmdHis");
		$info['name'] = $link;
		$info['type'] = $inf['type'];
		$info['size'] = $inf['size'];
		$info['height'] = $h;
		$info['width'] = $w;
		$info = serialize($info);
		$name = addslashes($name);
		if ($rewrite != 0) {
			$sql = $this->mysqlclass->mysql_my_query("select id FROM `".$this->setupdata['table_prefix']."files` where parent = '$parent' and types='image'", __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_num_rows($sql);
			if ($row != 0) {
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$this->image_del($parent, "", $row['id']);
			}
		}		
		$sql = "INSERT INTO `".$this->setupdata['table_prefix']."files` (`id`,`parent`,`name`,`info`,`save_date`,`owner`, `types`)	VALUES ('', '$parent', '$name', '$info', '$dat', '$usr', 'image')";
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		$i = $this->mysqlclass->mysql_my_insert_id();
		return $i;
	}

	function image_del_with_parent_data($parent, $name, $image_id){
		global $treeclass;
		if ($name == ""){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `name` FROM `".$this->setupdata['table_prefix']."data` where parent = '".$parent."' and value ='".$id."' and type='image'", __LINE__, __FILE__);
			$num = $this->mysqlclass->mysql_my_num_rows($sql);
			if ($num == 1){
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$treeclass->mod_item_data($parent, $row['name'], "");
			}
		} else $treeclass->mod_item_data($parent, $name, "");
		//$this->image_del($image_id);
	}
	
	function image_del($id) {
		global $setupdata, $treeclass;
		$id = intval($id);
		if ($id > 0) {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."files` WHERE '".$id."'=`id`", __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_num_rows($sql);
			if ($row != 0) {
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$parent = $row['parent'];
				$row = unserialize($row['info']);
				$files = $setupdata['binary_path']."/".$row['name'];
				if (!(file_exists($files))) {
					$setupdata['binary_path'] = $setupdata['binary_path']."/".$parent;
					$files = $setupdata['binary_path']."/".$row['name'];
				}
				if ((file_exists($files)) and (strlen($row['name']) > 0)){
					list($par, $d, $s) = explode("-", $row['name']);
					$pattern = $par."-".$d;
					if (is_dir($setupdata['binary_path'])){
						$d = opendir($setupdata['binary_path']);
						while ($file = readdir($d)){
							if (is_file($setupdata['binary_path']."/".$file)) {
								if (strpos($file, $pattern) === FALSE){
								} else{
									unlink($setupdata['binary_path']."/".$file);
								}
							}
						}
						closedir($d);
					}
				}
				$tmp = round($id / 250);
				$path_to_cache_file = $this->setupdata['binary_path']."/ids"."/".$tmp."/".$id;
				if (file_exists($path_to_cache_file)) unlink($path_to_cache_file);

				if (file_exists($files) === FALSE ){
					$this->mysqlclass->mysql_my_query("delete from `".$this->setupdata['table_prefix']."files` where id = $id", __LINE__, __FILE__);
				}
			}
		}
	}

	function image_get_link($id, $types){
		$id = intval($id);
		if ((strlen($id) > 0) && (intval($id) > 0)){
			$tmp = round($id / 250);
			
			$path_to_cache_file = $this->setupdata['binary_path']."/ids"."/".$tmp."/".$id;
					
			if (file_exists($path_to_cache_file)) {
				require $path_to_cache_file;
				if (isset($data_id)) {
					$rows['parent'] = $data_id['parent'];
					$data_id = $data_id['info'];
					$rows['link'] = $data_id['name'];
					$rows['size'] = $data_id['size'];
					$rows['mime'] = $data_id['type'];
					return $rows;
				}
			}
			
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."files` WHERE `id` = ".$id." AND `types`='".$types."'", __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_num_rows($sql);
			if ($row != 0) {
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$rows['parent'] = $row['parent'];
				$row1 = unserialize($row['info']);
				$rows['link'] = $row1['name'];
				$rows['size'] = $row1['size'];
				$rows['mime'] = $row1['type'];

				$path_to_cache_file = $this->setupdata['binary_path']."/ids";
	
				if (!file_exists($path_to_cache_file)) {
					mkdir($path_to_cache_file);
					chmod($path_to_cache_file, 0777);
				}
				
				$path_to_cache_file = $path_to_cache_file."/".$tmp;
				
				if (!file_exists($path_to_cache_file)) {
					mkdir($path_to_cache_file);
					chmod($path_to_cache_file, 0777);
				}
				$path_to_cache_file = $path_to_cache_file."/".$id;
				
				$handle = fopen($path_to_cache_file, "w");
				$row['info'] = unserialize($row['info']);
				$result = var_export($row, true); 
				fwrite($handle, "<?\n\$data_id = ");
				fwrite($handle, $result);
				fwrite($handle, ";\n?>");
				fclose($handle);

				return $rows;
			}
		}
		return NULL;
	}
	
	function image_mod_name($id, $name, $usr){
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."files` where id = '$id'", __LINE__, __FILE__);
		$row = $this->mysqlclass->mysql_my_num_rows($sql);
		if ($row == 1) {
			$dat = date("YmdHis");
			$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."files` SET name='".$name."', save_date ='".$dat."', owner ='".$usr."' WHERE id = ".$id, __LINE__, __FILE__);
		}
	}
	
	function image_size($id){
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."files` where id = '$id'", __LINE__, __FILE__);
		$row = $this->mysqlclass->mysql_my_num_rows($sql);
		if ($row != 0) {
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$row = unserialize($row['info']);
			$rows['width'] = $row['width'];
			$rows['height'] = $row['height'];
			return $rows;
		}
	}

	function image_sel($id, $paging=false) {
		$rows = array();
		if ($paging === false){
			if ($id > 0) $query="select * FROM `".$this->setupdata['table_prefix']."files` WHERE parent=".$id." and types = 'image' ORDER BY `save_date` DESC";
			else $query="select * FROM `".$this->setupdata['table_prefix']."files` WHERE types = 'image' ORDER BY `save_date` DESC";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
				$row['info'] = unserialize($row['info']);
				array_push($rows, $row);
			}
			return $rows;
		} else {
			$from = $paging['curent_page'] * $paging['items_per_page'];
			if ($id > 0) $str = " parent=".$id." and ";
			else $str = "";
			$query="SELECT * FROM `".$this->setupdata['table_prefix']."files` WHERE ".$str."types = 'image' ORDER BY `save_date` DESC LIMIT ".$from.", ".$paging['items_per_page'];
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
				$row['info'] = unserialize($row['info']);
				$rows['list'][] =  $row;
			}
			$query="SELECT COUNT(`id`) FROM `".$this->setupdata['table_prefix']."files` WHERE ".$str."types = 'image'";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			$row = $this->mysqlclass->mysql_my_fetch_array($sql);
			$rows['count'] = $row[0];
			return $rows;
		}
	}
	
	function is_animated_gif($filename){ 
		$lsd_offset = 6; // logical screen descriptor 
		$gct_offset = 13; // global color table 
		$chunk_size = 2048; // сколько читаем из начала файла 
		 
		$fd     = fopen($filename, 'r'); 
		$buff   = fread($fd, $chunk_size); 
		fclose($fd); 
	
		$packed     = ord($buff[$lsd_offset+4]); 
		$gct_flag   = ($packed >> 7) & 1; 
		$gct_size   = $packed & 7; 
		 
		$gct_length  = 1 << ($gct_size+1); 
		$data_offset = $gct_offset + ($gct_flag? 3*$gct_length: 0); 
	
		while ($data_offset < strlen($buff)) { 
			if ((ord($buff[$data_offset])==0x21) && (ord($buff[$data_offset+1]) == 0xf9)) {     
				// we hit a Graphic control extension 
				$delay_time = ord($buff[$data_offset+5])<<8 | ord($buff[$data_offset+4]); 
				if ($delay_time > 0) { 
					return true; 
				} else { 
					return false; 
				} 
				 
			} elseif((ord($buff[$data_offset])==0x21) && (ord($buff[$data_offset+1]) == 0xff)) { 
				// we hit an application extension 
				$app_name   = substr($buff, $data_offset+3, 8); 
				$app_bytes  = substr($buff, $data_offset+11, 3); 
				$app_data   = array(); 
				$data_offset += 14; 
				do { 
					$size = ord($buff[$data_offset]); 
					$app_data[] = substr($buff, $data_offset+1, $size); 
					$data_offset += $size+1; 
				} while (ord($buff[$data_offset]) !=0); 
				$data_offset += 1; 
				 
				// found Netscape looping extension. GIF is animated. 
				if (('NETSCAPE' == $app_name) && ('2.0' == $app_bytes) && (3 == strlen($app_data[0])) && (1 == ord($app_data[0][0]))) { 
					return true; 
				} 
				 
				 
			} elseif((ord($buff[$data_offset])==0x21) && (ord($buff[$data_offset+1]) == 0xfe)) { 
				// we hit a comment extension 
				$data_offset += 2; 
				do { 
					$size = ord($buff[$data_offset]); 
					$data_offset += $size+1; 
				} while (ord($buff[$data_offset]) !=0); 
				$data_offset += 1; 
				 
			} elseif(ord($buff[$data_offset]) == 0x2c) { 
				// we hit an actual image 
				return false; 
				 
			} else { 
				// shit happens 
				die ("GIF format error.<br>");             
			} 
		}     
	}

}
?>
